﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using System.Text.RegularExpressions;

namespace Business
{
    public static class ArticlesManager
    {
        #region Create (Add)

        public static void Create(Article article)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                article.Date = DateTime.Now;
                context.Article.AddObject(article);
                context.SaveChanges();
            }
        }

        #endregion

        #region Read (Get)

        public static List<Article> Read(int page = 1, int numberPerPage = 5)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = false;
                return context.Article.Include("Category")
                    .OrderByDescending(x => x.ID)
                    .Skip((page-1) * numberPerPage)
                    .Take(numberPerPage)
                    .ToList();
            }
        }

        public static List<Article> ReadAll()
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = false;
                return context.Article.Include("Category").Include("Comment").OrderByDescending(x => x.ID).ToList();
            }
        }

        public static Article ReadOne(int id)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                try
                {
                    return context.Article.Include("Category").Include("Comment").Single(x => x.ID == id);
                }catch(Exception e)
                { return null; }
            }
        }

        public static List<Article> ReadLast()
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                return context.Article.Include("Category").Include("Comment").OrderByDescending(x => x.ID).Take(2).ToList();
            }
        }

        public static List<Article> Search(string search)
        {
            List<Article> articles = ArticlesManager.ReadAll();
            List<Article> results = new List<Article>();
            RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
            Regex regex = new Regex(".*"+search+".*", options);

            foreach (Article a in articles)
            {
                if (regex.IsMatch(a.Content) || regex.IsMatch(a.Title))
                    results.Add(a);
            }
            return results;
        }

        #endregion

        #region Update (Set)

        public static void Update(Article article)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                Article a = new Article();
                a = context.Article.Single(x => x.ID == article.ID);
                a.Title = article.Title;
                a.Content = article.Content;
                a.CategoryID = article.CategoryID;

                context.SaveChanges();
            }
        }

        #endregion

        #region Delete (Delete)

        public static void Delete(Article article)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                context.Article.DeleteObject(article);
                context.SaveChanges();
            }
        }

        public static void Delete(int ID)
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                Article c = context.Article.Where(x => x.ID == ID).First(); ;
                context.Article.DeleteObject(c);
                context.SaveChanges();
            }
        }

        #endregion

        #region COUNT

        public static int Count()
        {
            using (BlogBDEntities context = new BlogBDEntities())
            {
                return context.Article.Count();
            }
        }

        #endregion
    }
}
